package com.wtpaint.lib.plugin.alipay;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;

import java.lang.reflect.Constructor;

public class ProxyActivity extends Activity {

    private String className;
    private IPayActivity payActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        className = getIntent().getStringExtra(Constants.INTENT_STRING_EXTRA_CLASS_NAME);

        try {
            Class activityClass = getClassLoader().loadClass(className);
            Constructor constructor = activityClass.getConstructor(new Class[]{});
            Object instance = constructor.newInstance(new Object[]{});

            payActivity = (IPayActivity) instance;
            payActivity.attach(this);

            Bundle bundle = new Bundle();
            payActivity.onCreate(bundle);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void startActivity(Intent intent) {
        String className = intent.getStringExtra(Constants.INTENT_STRING_EXTRA_CLASS_NAME);
        Intent intent1 = new Intent();
        intent1.putExtra(Constants.INTENT_STRING_EXTRA_CLASS_NAME, className);
        super.startActivity(intent1);
    }

    @Override
    public ComponentName startService(Intent service) {
        String serviceName = service.getStringExtra(Constants.INTENT_STRING_EXTRA_SERVICE_NAME);
        Intent intent = new Intent(this, ProxyService.class);
        intent.putExtra(Constants.INTENT_STRING_EXTRA_SERVICE_NAME, serviceName);
        return super.startService(intent);
    }

    @Override
    public ClassLoader getClassLoader() {
        return PluginManager.getInstance().getDexClassLoader();
    }

    @Override
    public Resources getResources() {
        return PluginManager.getInstance().getResource();
    }

    @Override
    protected void onStart() {
        super.onStart();
        payActivity.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        payActivity.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        payActivity.onDestroy();
    }
}
